package com.guyu.common.core.exception;

import com.guyu.common.core.entity.BizResult;
import com.guyu.common.core.exception.code.IErrorCode;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.io.Serial;

/**
 * <p> 业务异常 </p>
 */
@Getter
@EqualsAndHashCode(callSuper = true)
public class BizException extends BaseException {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 业务码
     */
    private final String bizCode;

    private BizException(Integer code, String message, String bizCode) {
        super(code, message);
        this.bizCode = bizCode;
    }

    /**
     * @param error 错误码枚举
     */
    public static BizException create(IErrorCode error) {
        return new BizException(error.getCode(), error.getMessage(), error.getBizCode());
    }

    /**
     * @param error   错误码
     * @param message 自定义错误信息
     */
    public static BizException create(IErrorCode error, String message) {
        return new BizException(error.getCode(), message, error.getBizCode());
    }

    /**
     * @param bizResult 错误结果集
     */
    public static BizException create(BizResult<?> bizResult) {
        return new BizException(bizResult.getCode(), bizResult.getMessage(), bizResult.getBizCode());
    }
}
